package jp.ac.u_tokyo.dch.action.top;

import javax.servlet.http.HttpServletRequest;

import jp.ac.u_tokyo.dch.core.db.DBConnection;
import jp.ac.u_tokyo.dch.model.PageInfo;
import jp.ac.u_tokyo.dch.model.PsiSearch;
import jp.ac.u_tokyo.dch.model.xml.psi.Psi;
import jp.ac.u_tokyo.dch.model.xml.psi.PsiObject;
import jp.ac.u_tokyo.dch.service.DaoServiceAware;
import jp.ac.u_tokyo.dch.service.PsiService;
import jp.ac.u_tokyo.dch.service.Transaction;
import jp.ac.u_tokyo.dch.util.DchCommonUtil;
import jp.ac.u_tokyo.dch.util.common.ModoruUtil;
import jp.ac.u_tokyo.dch.view.Constants;

import org.apache.commons.lang.xwork.StringUtils;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class PsiMaintenanceAction extends ActionSupport implements DaoServiceAware {


	/**
	 *
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * DBコネクションオブジェクト
	 */
	private DBConnection dbConnection = null;
	/**
	 * ポップアップサイン：１（ポップアップ）
	 */
	private static final String POPUP = "1";
	/**
	 * タイプ
	 */
	private String actionType;

	private PsiObject psiInfo;

	private PsiSearch search;

	private String fieldKey;
	private String index;
	private String kbn;

	/**
	 * ポップアップサイン
	 */
	private String popupSign = null;


	/**
	 * ページ状態保存オブジェクト
	 */
	private PageInfo<PsiObject> page;


	/**
	 * ポップアップを初期化する
	 * @return 結果
	 */
	public String init() {

		if (!POPUP.equals(popupSign)) {

			ModoruUtil.modoruProcess(ServletActionContext.getRequest(), "/topic/psiinit", ServletActionContext.getRequest().getQueryString());
		}

		return SUCCESS;
	}

	public String search() throws Exception {


		if (!POPUP.equals(popupSign)) {

			ModoruUtil.modoruProcess(ServletActionContext.getRequest(),
					"/topic/search",ServletActionContext.getRequest().getQueryString());
		}

		PsiService service = new PsiService();

		search.setCurrentPage("1");

		page = service.searchPsi(search);

		return SUCCESS;
	}

	/**
	 * 検索処理
	 * @return 結果
	 * @throws Exception 例外
	 */
	public String searchstate() throws Exception {

		if (!POPUP.equals(popupSign)) {
			ModoruUtil.modoruProcess(ServletActionContext.getRequest(),
					"/topic/searchstate",ServletActionContext.getRequest().getQueryString());
		}

		PsiService service = new PsiService();

		page = service.searchPsi(search);

		return SUCCESS;

	}


	public String add() {

		actionType = Constants.ADD;

		return SUCCESS;
	}

	/**
	 * 編集処理
	 * @return 結果
	 * @throws Exception 例外
	 */
	public String editInit() throws Exception {

		actionType = Constants.MOD;

		PsiService service = new PsiService();

		HttpServletRequest request = ServletActionContext.getRequest();
		String psi = request.getParameter("psi");

		Psi psiObj = service.searchByPsi(psi);

		psiInfo = psiObj.getResponse().getResponseData().getDataset().get(0);

		return SUCCESS;
	}

	/**
	 * コピー処理
	 * @return 結果
	 * @throws Exception 例外
	 */
	public String copyInit() throws Exception {

		actionType = Constants.ADD;

		PsiService service = new PsiService();

		HttpServletRequest request = ServletActionContext.getRequest();
		String psi = request.getParameter("psi");

		Psi psiObj = service.searchByPsi(psi);

		psiInfo = psiObj.getResponse().getResponseData().getDataset().get(0);

		return SUCCESS;
	}
	/**
	 * 削除処理
	 * @return 結果
	 * @throws Exception 例外
	 */
	public String deleteInit() throws Exception {

		actionType = Constants.DEL;

		PsiService service = new PsiService();

		HttpServletRequest request = ServletActionContext.getRequest();
		String psi = request.getParameter("psi");

		Psi psiObj = service.searchByPsi(psi);

		psiInfo = psiObj.getResponse().getResponseData().getDataset().get(0);

		return SUCCESS;
	}

	/**
	 * 確認画面から戻る処理
	 * @return 結果
	 * @throws Exception 例外
	 */
	public String modoru() throws Exception {

		return SUCCESS;
	}

	/**
	 * 確認処理
	 * @return 結果
	 * @throws Exception 例外
	 */
	public String confirm() throws Exception {

		if (StringUtils.isEmpty(psiInfo.getName_jp())) {
			addFieldError("psiInfo.name_jp", getText("requiredstring", new String[] { "日本語名" }));
		}
		if (StringUtils.isEmpty(psiInfo.getBirth())) {
			addFieldError("psiInfo.birth", getText("requiredstring", new String[] { "生年月日" }));
		}
		if (StringUtils.isEmpty(psiInfo.getDeath())) {
			addFieldError("psiInfo.death", getText("requiredstring", new String[] { "没年月日" }));
		}

		if (hasErrors()) {
			return ERROR;
		}

		if (StringUtils.isNotEmpty(psiInfo.getPsi()) && !DchCommonUtil.urlCheck(psiInfo.getPsi())) {

			addFieldError("psiInfo.psi", getText("urlcheck"));
			return ERROR;
		}


		if (!DchCommonUtil.checkDate(psiInfo.getBirth())) {

			addFieldError("psiInfo.birth", getText("hizukecheck"));
			return ERROR;
		}

		if (!DchCommonUtil.checkDate(psiInfo.getDeath())) {

			addFieldError("psiInfo.death", getText("hizukecheck"));
			return ERROR;
		}

		return SUCCESS;
	}

	/**
	 * 登録、削除、編集処理
	 * @return 結果
	 * @throws Exception 例外
	 */
	@Transaction
	public String execute() throws Exception {

		PsiService service = new PsiService();

		if (Constants.ADD.equals(actionType)) {

			Psi resultPsi = service.insert(dbConnection, psiInfo);

			if (Constants.PSI_ERR.equals(resultPsi.getResponse().getStatus())) {

				if (Constants.PSI_ERR_DUPLICATE_ERROR.equals(resultPsi.getResponse().getResponseData().getDetail())) {
					addFieldError("id", getText("duplicatePsi"));
				} else {
					addFieldError("id", getText("psiSystemError"));
				}
				return ERROR;
			}

		} else if (Constants.MOD.equals(actionType)) {

			Psi resultPsi = service.update(dbConnection, psiInfo);

			if (Constants.PSI_ERR.equals(resultPsi.getResponse().getStatus())) {

				if (resultPsi.getResponse().getResponseData().getDetail() == null) {
					addFieldError("id", getText("psiSystemError"));
				} else if (Constants.PSI_ERR_VERSION_MISMATCH_ERROR.equals(resultPsi.getResponse().getResponseData().getDetail())) {
					addFieldError("id", getText("versionmismatch"));
				} else {
					addFieldError("id", getText("psiSystemError"));
				}
				return ERROR;
			}

		} else if (Constants.DEL.equals(actionType)) {

			Psi resultPsi = service.delete(psiInfo);

			if (Constants.PSI_ERR.equals(resultPsi.getResponse().getStatus())) {

				if (resultPsi.getResponse().getResponseData().getDetail() == null) {
					addFieldError("id", getText("psiSystemError"));
				}else if (Constants.PSI_ERR_VERSION_MISMATCH_ERROR.equals(resultPsi.getResponse().getResponseData().getDetail())) {
					addFieldError("id", getText("versionmismatch"));
				} else {
					addFieldError("id", getText("psiSystemError"));
				}
				return ERROR;
			}
		}

		return SUCCESS;
	}


	/**
	 * DBコネクションオブジェクトをセットする
	 */
	@Override
	public void setDBConnection(DBConnection con) {
		dbConnection = con;
	}

	public String getActionType() {
		return actionType;
	}

	public void setActionType(String actionType) {
		this.actionType = actionType;
	}

	public PsiObject getPsiInfo() {
		return psiInfo;
	}

	public void setPsiInfo(PsiObject psiInfo) {
		this.psiInfo = psiInfo;
	}

	public PsiSearch getSearch() {
		return search;
	}

	public void setSearch(PsiSearch search) {
		this.search = search;
	}

	public PageInfo<PsiObject> getPage() {
		return page;
	}

	public void setPage(PageInfo<PsiObject> page) {
		this.page = page;
	}

	public String getFieldKey() {
		return fieldKey;
	}

	public void setFieldKey(String fieldKey) {
		this.fieldKey = fieldKey;
	}

	public String getIndex() {
		return index;
	}

	public void setIndex(String index) {
		this.index = index;
	}

	public String getKbn() {
		return kbn;
	}

	public void setKbn(String kbn) {
		this.kbn = kbn;
	}

	public String getPopupSign() {
		return popupSign;
	}

	public void setPopupSign(String popupSign) {
		this.popupSign = popupSign;
	}


}
